package Client;

import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.print.attribute.standard.Severity;

import Server.Game;

import common.ClientInterface;
import common.ServerInterface;
import common.TurnInterface;

public class mainClient {
	
	public static void main(String args[]){
		
		String man = ("Parameter's list:\n" +
				"t - show a textual interface\n" +
				"g - show a graphical interface\n" +
				"\n" +
				"l - local communication with the server\n" +
				"r - RMI communication with the server\n" +
				"s - socket communication with the server\n" +
				"\n" +
		"in case of RMI or socket server's address and port are needed\n");
		UI view =  null;
		ServerInterface server = null;
		Client client = null;
		
		try{
			if (args[1].equalsIgnoreCase("l")){
				Game game = Game.getGame();
				client = new Client(game, null);
				//game.addClient(client);
			}
			else if (args[1].equalsIgnoreCase("r")) {
				if(args.length==4){
				server = new RmiServer(args[2]); // INDIRIZZO
				client = new Client(server, (TurnInterface)server );}
			}
			else if (args[1].equalsIgnoreCase("s")) {
				if(args.length==4){
				server = new SocketServer(args[2],Integer.valueOf(args[3]).intValue());
				client = new Client(server, (TurnInterface)server );
				}
			}
			else {
				System.out.println("Option Error\n" + man);
				System.exit(0);
			}
		} catch (ArrayIndexOutOfBoundsException e){
			System.out.println("Option Error\n" + man);
			System.exit(0);
		} catch (RemoteException e){
			System.out.println("Errore di comunicazione con il server");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try{
			if (args[0].equalsIgnoreCase("f")){
				view = new FGUI(client);
			}
			else if (args[0].equalsIgnoreCase("g")){view = new FGUI(client);}
			else {	System.out.println("Option Error\n" + man);
			System.exit(0);
			}
		} catch (ArrayIndexOutOfBoundsException e){
			System.out.println("Option Error" + man);
			System.exit(0);
		}
		
		client.setView(view);
		view.showPreLogin();
	}
	
}